<?php

namespace app\data\controller\api;

use app\data\service\CarService;
use think\admin\Controller;

/**
 * 基础数据接口
 * Class Data
 * @package app\data\controller\api
 */
class Data extends Controller
{

    /**
     * 获取指定数据对象
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getData()
    {
        $data = $this->_vali(['name.require' => '数据名称不能为空！']);
        $this->success('获取数据对象', sysdata($data['name']));
    }

    /**
     * @api            {get} api/home
     * @apiName        获取轮播图片数据
     * @apiStatus      todo
     * @apiDescription 获取轮播图片数据-描述
     * @apiVersion     1.0.0
     * @apiPermission  token
     * @apiParam {integer} user_id=0 用户id
     * @apiParam {object} data={car_base_price}
     * @apiSuccess {object} data={car_base_price}
     * @apiSuccess {integer} data.new_name 名称
     * @apiSuccess {integer} data.new_name2=123 名称
     */
    public function getSlider()
    {
        $data = sysdata(input('keys', 'slider_home'));
        $this->success('获取轮播图片', $data);
    }

    /**
     * 获取牛超市配置(cattle_barrage=牛超市弹幕;cattle_phone=牛超市电话;mall_phone=商城电话)
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCattleConfig()
    {
        $cattleKeys = ['cattle_barrage', 'cattle_phone', 'mall_phone'];
        $cattleKey = input('keys', 'cattle_barrage');
        if (!in_array($cattleKey, $cattleKeys)) {
            $this->error('参数错误');
        }
        $data = sysconf('data.' . $cattleKey);
        $this->success('获取牛超市配置', explode('#', $data));
    }

    /**
     * 获取系统通知数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNotify()
    {
        $query = $this->_query('BaseUserMessage')->where(['status' => 1, 'deleted' => 0]);
        $result = $query->equal('id')->order('sort desc,id desc')->page(true, false, false, 20);
        if (($id = input('id')) > 0) {
            $this->app->db->name('BaseUserMessage')->where(['id' => $id])->inc('num_read')->update();
        }
        $this->success('获取系统通知', $result);
    }

    /**
     * 获取平台通知
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function platformMessage()
    {
        $query = $this->_query('BasePlatformMessage')->where(['status' => 1, 'deleted' => 0]);
        $result = $query->equal('id')->order('sort desc,id desc')->page(true, false, false, 20);
        if (($id = input('id')) > 0) {
            $this->app->db->name('BasePlatformMessage')->where(['id' => $id])->inc('num_read')->update();
        }
        $this->success('获取系统通知', $result);
    }

    /**
     * 版本信息
     */
    public function version()
    {
        $this->success('获取版本信息', [
            'is_like' => 0,
            'version' => sysconf('app_version'),
            'need_update' => 0,
            'ios' => 'https://www.gzplat.com/p/a31ez',
            'android' => 'https://www.gzplat.com/p/GnJ39',
        ]);
    }

    /**
     * 订单退款原因
     */
    public function refundReason()
    {
        $this->success('获取订单退款原因', [
            '商品质量问题',
            '收到商品破损',
            '商品错发/漏发',
            '收到商品描述不符' ,
            '未按约定时间发货',
            '不想要了',
            '其他'
        ]);
    }

    /**
     * 增加费用项列表
     * @return void
     */
    public function addPriceType()
    {
        $this->success('获取费用项列表成功', CarService::instance()->addPriceType());
    }

    /**
     * 获取欢迎页图片
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWelcome()
    {
        $data = sysconf('data.welcome_page');
        $this->success('获取欢迎页图片', $data);
    }

    /**
     * @api            {get} api/cs-phone
     * @apiName        获取客服电话
     * @apiStatus      working
     * @apiDescription 获取客服电话-描述
     */
    public function getCsPhone()
    {
        $data = sysconf('data.cs_phone');
        $this->success('获取客服电话', $data);
    }
}